//
//  XSegmentedControl.m
//  XDBaseFrameWork
//
//  Created by XD-XY on 9/20/14.
//  Copyright (c) 2014 XD-XY. All rights reserved.
//

#import "XSegmentedControl.h"

@implementation XSegmentedControl

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self initViews];
    }
    return self;
}

-(id)initWithItems:(NSArray *)items
{
    self = [super initWithItems:items];
    if (self){
        [self initViews];
    }
    return self;
}

-(void)initViews
{

    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.f){
        
        NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:16],NSFontAttributeName,nil];
        
        [self setTitleTextAttributes:dic forState:UIControlStateNormal];
    }else{
    NSDictionary *selectedAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                        [UIFont systemFontOfSize:16], UITextAttributeFont,
                                        UIColorFromRGB(0xff6800),UITextAttributeTextColor,
                                        [UIColor clearColor], UITextAttributeTextShadowColor,
                                        nil];
    [self setTitleTextAttributes:selectedAttributes forState:UIControlStateNormal];

    NSDictionary *normalAttributes = [NSDictionary dictionaryWithObjectsAndKeys:
                                      [UIFont systemFontOfSize:16], UITextAttributeFont,
                                      [UIColor whiteColor], UITextAttributeTextColor,
                                      [UIColor clearColor], UITextAttributeTextShadowColor,
                                      nil];
    [self setTitleTextAttributes:normalAttributes forState:UIControlStateSelected];
        
    /* Unselected background */
    UIImage *unselectedBackgroundImage = [[UIImage
                                           imageNamed:@"segment-unselected-item"]
                                          resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    [self setBackgroundImage:unselectedBackgroundImage
                                               forState:UIControlStateNormal
                                             barMetrics:UIBarMetricsDefault];
    
    /* Selected background */
    UIImage * selectedBackgroundImage = [[UIImage
                                         imageNamed:@"segment-selected-item"]
                                        resizableImageWithCapInsets:UIEdgeInsetsMake(10, 10, 10, 10)];
    [self setBackgroundImage:selectedBackgroundImage
                                               forState:UIControlStateSelected
                                             barMetrics:UIBarMetricsDefault];
    
    /* Image between two unselected segments */
    UIImage * bothUnselectedImage = [[UIImage
                                     imageNamed:@"segment-middle"]
                                    resizableImageWithCapInsets:UIEdgeInsetsMake(15, 0, 15, 0)];
    [self setDividerImage:bothUnselectedImage
                                 forLeftSegmentState:UIControlStateNormal
                                   rightSegmentState:UIControlStateNormal
                                          barMetrics:UIBarMetricsDefault];
    
    /* Image between segment selected on the left and unselected on the right */
    UIImage *leftSelectedImage = [[UIImage
                                   imageNamed:@"segment-middle"]
                                  resizableImageWithCapInsets:UIEdgeInsetsMake(15, 0, 15, 0)];
    [self setDividerImage:leftSelectedImage
                                 forLeftSegmentState:UIControlStateSelected
                                   rightSegmentState:UIControlStateNormal
                                          barMetrics:UIBarMetricsDefault];
    
    /* Image between segment selected on the right and unselected on the left */
    UIImage *rightSelectedImage = [[UIImage
                                    imageNamed:@"segment-middle"]
                                   resizableImageWithCapInsets:UIEdgeInsetsMake(15, 0, 15, 0)];
    [self setDividerImage:rightSelectedImage
                                 forLeftSegmentState:UIControlStateNormal
                                   rightSegmentState:UIControlStateSelected
                                          barMetrics:UIBarMetricsDefault];
        
    }
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
